1 // Debugging deque implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
27 * This file is a GNU debug extension to the Standard C++ Library.
30 #ifndef _GLIBCXX_DEBUG_DEQUE
31 #define _GLIBCXX_DEBUG_DEQUE 1
34 #include <debug/safe_sequence.h>
35 #include <debug/safe_iterator.h>
41 template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
43 : public _GLIBCXX_STD_D::deque<_Tp, _Allocator>,
44 public __gnu_debug::_Safe_sequence<deque<_Tp, _Allocator> >
46 typedef _GLIBCXX_STD_D::deque<_Tp, _Allocator> _Base;
47 typedef __gnu_debug::_Safe_sequence<deque> _Safe_base;
50 typedef typename _Base::reference reference;
51 typedef typename _Base::const_reference const_reference;
53 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,deque>
55 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,deque>
58 typedef typename _Base::size_type size_type;
59 typedef typename _Base::difference_type difference_type;
61 typedef _Tp value_type;
62 typedef _Allocator allocator_type;
63 typedef typename _Base::pointer pointer;
64 typedef typename _Base::const_pointer const_pointer;
65 typedef std::reverse_iterator<iterator> reverse_iterator;
66 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
68 // 23.2.1.1 construct/copy/destroy:
69 explicit deque(const _Allocator& __a = _Allocator())
72 explicit deque(size_type __n, const _Tp& __value = _Tp(),
73 const _Allocator& __a = _Allocator())
74 : _Base(__n, __value, __a) { }
76 template<class _InputIterator>
77 deque(_InputIterator __first, _InputIterator __last,
78 const _Allocator& __a = _Allocator())
79 : _Base(__gnu_debug::__check_valid_range(__first, __last), __last, __a)
82 deque(const deque& __x)
83 : _Base(__x), _Safe_base() { }
85 deque(const _Base& __x)
86 : _Base(__x), _Safe_base() { }
88 #ifdef __GXX_EXPERIMENTAL_CXX0X__
90 : _Base(std::forward<deque>(__x)), _Safe_base()
91 { this->_M_swap(__x); }
93 deque(initializer_list<value_type> __l,
94 const allocator_type& __a = allocator_type())
95 : _Base(__l, __a), _Safe_base() { }
101 operator=(const deque& __x)
103 *static_cast<_Base*>(this) = __x;
104 this->_M_invalidate_all();
108 #ifdef __GXX_EXPERIMENTAL_CXX0X__
110 operator=(deque&& __x)
119 operator=(initializer_list<value_type> __l)
121 *static_cast<_Base*>(this) = __l;
122 this->_M_invalidate_all();
127 template<class _InputIterator>
129 assign(_InputIterator __first, _InputIterator __last)
131 __glibcxx_check_valid_range(__first, __last);
132 _Base::assign(__first, __last);
133 this->_M_invalidate_all();
137 assign(size_type __n, const _Tp& __t)
139 _Base::assign(__n, __t);
140 this->_M_invalidate_all();
143 #ifdef __GXX_EXPERIMENTAL_CXX0X__
145 assign(initializer_list<value_type> __l)
148 this->_M_invalidate_all();
152 using _Base::get_allocator;
157 { return iterator(_Base::begin(), this); }
161 { return const_iterator(_Base::begin(), this); }
165 { return iterator(_Base::end(), this); }
169 { return const_iterator(_Base::end(), this); }
173 { return reverse_iterator(end()); }
175 const_reverse_iterator
177 { return const_reverse_iterator(end()); }
181 { return reverse_iterator(begin()); }
183 const_reverse_iterator
185 { return const_reverse_iterator(begin()); }
187 #ifdef __GXX_EXPERIMENTAL_CXX0X__
190 { return const_iterator(_Base::begin(), this); }
194 { return const_iterator(_Base::end(), this); }
196 const_reverse_iterator
198 { return const_reverse_iterator(end()); }
200 const_reverse_iterator
202 { return const_reverse_iterator(begin()); }
205 // 23.2.1.2 capacity:
207 using _Base::max_size;
210 resize(size_type __sz, _Tp __c = _Tp())
212 typedef typename _Base::const_iterator _Base_const_iterator;
213 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
215 bool __invalidate_all = __sz > this->size();
216 if (__sz < this->size())
217 this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
219 _Base::resize(__sz, __c);
221 if (__invalidate_all)
222 this->_M_invalidate_all();
229 operator[](size_type __n)
231 __glibcxx_check_subscript(__n);
232 return _M_base()[__n];
236 operator[](size_type __n) const
238 __glibcxx_check_subscript(__n);
239 return _M_base()[__n];
247 __glibcxx_check_nonempty();
248 return _Base::front();
254 __glibcxx_check_nonempty();
255 return _Base::front();
261 __glibcxx_check_nonempty();
262 return _Base::back();
268 __glibcxx_check_nonempty();
269 return _Base::back();
272 // 23.2.1.3 modifiers:
274 push_front(const _Tp& __x)
276 _Base::push_front(__x);
277 this->_M_invalidate_all();
281 push_back(const _Tp& __x)
283 _Base::push_back(__x);
284 this->_M_invalidate_all();
287 #ifdef __GXX_EXPERIMENTAL_CXX0X__
289 push_front(_Tp&& __x)
290 { emplace_front(std::move(__x)); }
294 { emplace_back(std::move(__x)); }
296 template<typename... _Args>
298 emplace_front(_Args&&... __args)
300 _Base::emplace_front(std::forward<_Args>(__args)...);
301 this->_M_invalidate_all();
304 template<typename... _Args>
306 emplace_back(_Args&&... __args)
308 _Base::emplace_back(std::forward<_Args>(__args)...);
309 this->_M_invalidate_all();
312 template<typename... _Args>
314 emplace(iterator __position, _Args&&... __args)
316 __glibcxx_check_insert(__position);
317 typename _Base::iterator __res = _Base::emplace(__position.base(),
318 std::forward<_Args>(__args)...);
319 this->_M_invalidate_all();
320 return iterator(__res, this);
325 insert(iterator __position, const _Tp& __x)
327 __glibcxx_check_insert(__position);
328 typename _Base::iterator __res = _Base::insert(__position.base(), __x);
329 this->_M_invalidate_all();
330 return iterator(__res, this);
333 #ifdef __GXX_EXPERIMENTAL_CXX0X__
335 insert(iterator __position, _Tp&& __x)
336 { return emplace(__position, std::move(__x)); }
339 insert(iterator __p, initializer_list<value_type> __l)
341 _Base::insert(__p, __l);
342 this->_M_invalidate_all();
347 insert(iterator __position, size_type __n, const _Tp& __x)
349 __glibcxx_check_insert(__position);
350 _Base::insert(__position.base(), __n, __x);
351 this->_M_invalidate_all();
354 template<class _InputIterator>
356 insert(iterator __position,
357 _InputIterator __first, _InputIterator __last)
359 __glibcxx_check_insert_range(__position, __first, __last);
360 _Base::insert(__position.base(), __first, __last);
361 this->_M_invalidate_all();
367 __glibcxx_check_nonempty();
368 iterator __victim = begin();
369 __victim._M_invalidate();
376 __glibcxx_check_nonempty();
377 iterator __victim = end();
379 __victim._M_invalidate();
384 erase(iterator __position)
386 __glibcxx_check_erase(__position);
387 if (__position == begin() || __position == end()-1)
389 __position._M_invalidate();
390 return iterator(_Base::erase(__position.base()), this);
394 typename _Base::iterator __res = _Base::erase(__position.base());
395 this->_M_invalidate_all();
396 return iterator(__res, this);
401 erase(iterator __first, iterator __last)
403 // _GLIBCXX_RESOLVE_LIB_DEFECTS
404 // 151. can't currently clear() empty container
405 __glibcxx_check_erase_range(__first, __last);
406 if (__first == begin() || __last == end())
408 this->_M_detach_singular();
409 for (iterator __position = __first; __position != __last; )
411 iterator __victim = __position++;
412 __victim._M_invalidate();
416 return iterator(_Base::erase(__first.base(), __last.base()),
421 this->_M_revalidate_singular();
422 __throw_exception_again;
427 typename _Base::iterator __res = _Base::erase(__first.base(),
429 this->_M_invalidate_all();
430 return iterator(__res, this);
435 #ifdef __GXX_EXPERIMENTAL_CXX0X__
449 this->_M_invalidate_all();
453 _M_base() { return *this; }
456 _M_base() const { return *this; }
459 template<typename _Tp, typename _Alloc>
461 operator==(const deque<_Tp, _Alloc>& __lhs,
462 const deque<_Tp, _Alloc>& __rhs)
463 { return __lhs._M_base() == __rhs._M_base(); }
465 template<typename _Tp, typename _Alloc>
467 operator!=(const deque<_Tp, _Alloc>& __lhs,
468 const deque<_Tp, _Alloc>& __rhs)
469 { return __lhs._M_base() != __rhs._M_base(); }
471 template<typename _Tp, typename _Alloc>
473 operator<(const deque<_Tp, _Alloc>& __lhs,
474 const deque<_Tp, _Alloc>& __rhs)
475 { return __lhs._M_base() < __rhs._M_base(); }
477 template<typename _Tp, typename _Alloc>
479 operator<=(const deque<_Tp, _Alloc>& __lhs,
480 const deque<_Tp, _Alloc>& __rhs)
481 { return __lhs._M_base() <= __rhs._M_base(); }
483 template<typename _Tp, typename _Alloc>
485 operator>=(const deque<_Tp, _Alloc>& __lhs,
486 const deque<_Tp, _Alloc>& __rhs)
487 { return __lhs._M_base() >= __rhs._M_base(); }
489 template<typename _Tp, typename _Alloc>
491 operator>(const deque<_Tp, _Alloc>& __lhs,
492 const deque<_Tp, _Alloc>& __rhs)
493 { return __lhs._M_base() > __rhs._M_base(); }
495 template<typename _Tp, typename _Alloc>
497 swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
498 { __lhs.swap(__rhs); }
500 #ifdef __GXX_EXPERIMENTAL_CXX0X__
501 template<typename _Tp, typename _Alloc>
503 swap(deque<_Tp, _Alloc>&& __lhs, deque<_Tp, _Alloc>& __rhs)
504 { __lhs.swap(__rhs); }
506 template<typename _Tp, typename _Alloc>
508 swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>&& __rhs)
509 { __lhs.swap(__rhs); }
512 } // namespace __debug